package com.modules.controller.sys;


import com.common.controller.BaseController;
import com.common.log.LogData;
import com.common.vo.Result;
import com.modules.entity.sys.OrganizationEntity;
import com.modules.service.sys.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 组织控制层
 * @author yyq
 *
 */
@Api(value="/sys/organization", tags = "机构管理")
@RestController
@RequestMapping("/sys/organization")
public class OrganizationController extends BaseController<OrganizationEntity>{
	
	@Resource
	private OrganizationService service;


	/**
	 * 查询树形表格
	 * @return
	 */
	@GetMapping("/selectTreeTable")
	@ResponseBody
	@ApiOperation(value = "查询树形表格", notes = "查询树形表格" , httpMethod = "GET")
	public Result<?> selecTreeTable(OrganizationEntity entity){
		entity.setOid(entity.getOid() == null ? getUser().getLastOid() : entity.getOid());
		return service.selecTreeTable(entity);
	}

	/**
	 * 获取树形所有
	 * @return
	 */
	@GetMapping("/selectTreeAll")
	@ResponseBody
	@ApiOperation(value = "查询树形所有", notes = "查询树形所有" , httpMethod = "GET")
	public Result<?> selectTreeAll(OrganizationEntity entity){
		entity.setOid(entity.getOid() == null ? getUser().getLastOid() : entity.getOid());
		return service.selectTreeAll(entity);
	}

	/**
	 * 查询单个
	 * @return
	 */
	@GetMapping("/selectById")
	@ApiOperation(value = "查询单个", notes = "查询单个" , httpMethod = "GET")
	public Result<?> selectById(OrganizationEntity entity){
		OrganizationEntity byId = service.getById(entity.getOid());
		return Result.success(byId, Result.LOAD_MSG);
	}

	/**
	 * 保存
	 * @return
	 */
	@PostMapping("/save")
	@LogData(module = "组织管理", remarks = "保存")
	@ApiOperation(value = "保存", notes = "保存" , httpMethod = "POST")
	public Result<?> save(OrganizationEntity entity){
		service.saveOrUpdate(entity);
		return entity.getOid() == null ? Result.success(Result.ADD_MSG) : Result.success(Result.UPDATE_MSG);
	}
	
	/**
	 * 删除
	 * @return
	 */
	@LogData(module = "组织管理", remarks = "删除单个")
	@DeleteMapping("/delete")
	@ApiOperation(value = "删除单个", notes = "删除单个" , httpMethod = "DELETE")
	public Result<?> delete(OrganizationEntity entity) {
		if(entity.getOid().equals(getUser().getOid())){
			return Result.fail("权限不足，删除失败");
		}
		service.removeById(entity.getOid());
		return Result.success(Result.DELETE_MSG);
	}

	/**
	 * 更改状态
	 * @return
	 */
	@PostMapping("/updateState")
	@ResponseBody
	public Result<?> updateState(OrganizationEntity entity){
		return service.updateState(entity);
	}




}